//
//  RGMessageView.m
//  RedGrass
//
//  Created by wukong on 2018/11/25.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMessageView.h"
#import "RGMessageFuncCell.h"
#import "RGMessageListCell.h"
#import "RGMessageNavView.h"


@interface RGMessageView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) RGMessageNavView *navView;
@property (strong,nonatomic) NSMutableArray *dataList;
@property (nonatomic,strong) MJRefreshHeader *mjHeader;
@property (nonatomic,strong) MJRefreshFooter *mjFooter;
@property (nonatomic,assign) NSInteger pageNum;

@end

@implementation RGMessageView

-(instancetype) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor blackColor];
        [self addSubview:self.navView];
        [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).mas_offset(44);
            make.left.and.right.mas_equalTo(self).mas_offset(0);
            make.height.mas_equalTo(50);
        }];
        [self addSubview:self.tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.navView.mas_bottom).mas_offset(0);
            make.left.and.right.mas_equalTo(self).mas_offset(0);
            make.bottom.mas_equalTo(self).mas_offset(0);
        }];
    }
    return self;
}

- (void)reloadTableView:(NSMutableArray *)dataList {
    self.dataList = dataList;
    [self.tableView reloadData];
}

- (void)beginRefreshing {
    [self.mjHeader beginRefreshing];
}

- (void)headerEndRefreshing {
    [self.mjHeader endRefreshing];
}

- (void)footerEndRefreshing {
    [self.mjFooter endRefreshing];
}

- (void)footerEndRefreshingWithNoData {
    [self.mjFooter endRefreshingWithNoMoreData];
}

- (void)loadFirstPage {
    self.pageNum = 1;
    if (self.delegate && [self.delegate respondsToSelector:@selector(loadFirstPageWithView:pageNum:)]) {
        [self.delegate loadFirstPageWithView:self pageNum:self.pageNum];
    }
}

- (void)loadNextPage {
    self.pageNum += 1;
    if (self.delegate && [self.delegate respondsToSelector:@selector(loadNextPageWithView:pageNum:)]) {
        [self.delegate loadNextPageWithView:self pageNum:self.pageNum];
    }
}

#pragma mark - tabelView delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }
    return self.dataList.count;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return 120;
    }
    return 75;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        RGMessageFuncCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RGMessageFuncCell" forIndexPath:indexPath];
        AA_WEAK_MAKE(self) weakSelf = self;
        [cell setActionBtnClick:^(NSInteger tag) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(funcItemChosedWithView:chosedTag:)]) {
                [weakSelf.delegate funcItemChosedWithView:weakSelf chosedTag:tag];
            }
        }];
        //自己都觉得奇怪的写法
        [cell addAction];
        return cell;
    }else{
        RGMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RGMessageListCell" forIndexPath:indexPath];
        cell.model = [self.dataList objectAtIndex:indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
    }else {
        if([self.delegate respondsToSelector:@selector(cellChosedWithView:chosedIndexPath:)]){
            [self.delegate cellChosedWithView:self chosedIndexPath:indexPath];
        }
    }
}


#pragma mark - getter
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor blackColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[RGMessageFuncCell class] forCellReuseIdentifier:@"RGMessageFuncCell"];
        [_tableView registerClass:[RGMessageListCell class] forCellReuseIdentifier:@"RGMessageListCell"];
        _tableView.mj_header = self.mjHeader;
        _tableView.mj_footer = self.mjFooter;
    }
    return _tableView;
}

-(RGMessageNavView *)navView{
    if(!_navView){
        _navView = [[RGMessageNavView alloc]init];
        AA_WEAK_MAKE(self)weakSelf = self;
        [_navView setContactBtnClick:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(contactBtnChosedWithView:)]) {
                [weakSelf.delegate contactBtnChosedWithView:weakSelf];
            }
        }];
    }
    return _navView;
}


- (MJRefreshHeader *)mjHeader {
    if (!_mjHeader) {
        _mjHeader = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadFirstPage)];
    }
    return _mjHeader;
}

- (MJRefreshFooter *)mjFooter {
    if (!_mjFooter) {
        _mjFooter = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNextPage)];
    }
    return _mjFooter;
}

@end
